/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista.Ventanas.PersonaLegal;

import Logica.Core.PersonaLegal.Cliente;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import Vista.Controladoras.ControladoraVista;
import Vista.Herramientas.CajaDeHerramientas;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luciano
 */
public class ABMClientes extends javax.swing.JInternalFrame {

    protected CajaDeHerramientas misHerramientas;
    protected Cliente miCliente;
    protected ControladoraVista miControladora;
    protected boolean filaSeleccionada;
    private boolean[] goodToGo;

    /**
     * Creates new form ABMClientes
     */
    public ABMClientes(ControladoraVista miControladora) {
        inicializar(miControladora);
        initComponents();
        cargarTabla();
    }

    private void inicializar(ControladoraVista miControladora) {
        misHerramientas = new CajaDeHerramientas();
        this.miControladora = miControladora;
        goodToGo = new boolean[2];
    }

    private void cargarTabla() {
        Object[] misDatos;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        DefaultTableModel temp = (DefaultTableModel) tblCliente.getModel();

        List<Cliente> misClientes = miControladora.getMiVistaCliente().dameMisClientes();


        temp.setRowCount(0);
        temp.setColumnCount(8);



        for (int i = 0; i < misClientes.size(); i++) {

            misDatos = new Object[9];
            misDatos[0] = misClientes.get(i).getNroCliente();
            misDatos[1] = misClientes.get(i).getNombre();
            misDatos[2] = misClientes.get(i).getApellido();
            misDatos[3] = misClientes.get(i).getDni();
            misDatos[4] = sdf.format(misClientes.get(i).getFechaNacimiento());
            misDatos[5] = misClientes.get(i).getDireccion();
            misDatos[6] = misClientes.get(i).getTelefono();
            misDatos[7] = misClientes.get(i).getEmail();



            if (misClientes.get(i).isEstado()) {
                misDatos[8] = "Alta";
            } else {
                misDatos[8] = "Baja";
            }

            if (misClientes.get(i).isEstado()) {
                temp.addRow(misDatos);
            } else {
                if (chkMostrarBajas.isSelected()) {
                    temp.addColumn("Estado");
                    temp.addRow(misDatos);
                }
            }
        }
        misHerramientas.getAutoAjustador().autoAjustar(tblCliente);
    }

    public void llenarCampos() {
        int i = tblCliente.getSelectedRow();
        if (i != -1) {
            for (int j = 0; j < 8; j++) {
                Object celda = tblCliente.getValueAt(i, j);
                if (celda != null) {
                    agregarAlCampo(j, celda.toString());
                }
            }
        }

    }

    private void agregarAlCampo(int indice, String celda) {
        switch (indice) {
            case 0:
                txtNroCliente.setText(celda);
                break;
            case 1:
                txtNombre.setText(celda);
                break;
            case 2:
                txtApellido.setText(celda);
                break;
            case 3:
                txtDNI.setText(celda);
                break;
            case 4:
                txtFechaNac.setText(celda);
                break;
            case 5:
                txtDireccion.setText(celda);
                break;
            case 6:
                txtTelefono.setText(celda);
                break;
            case 7:
                txtEmail.setText(celda);
                break;
        }
    }

    private void vaciarCampos() {
        for (int j = 0; j < 12; j++) {
            agregarAlCampo(j, "");
        }
    }

    private boolean datosValidos(JTextField[] obligatorios) throws Exception {
        boolean ret = true;

        for (int i = 0; i < obligatorios.length && ret; i++) {
            if (obligatorios[i].getText().equals("")) {
                ret = false;
            }
        }

        if (!ret) {
            throw new Exception("Campos obligatorios vacios.");
        }

        return ret;
    }

    private JTextField[] camposObligatorios() {
        JTextField[] obligatorios = new JTextField[2];


        obligatorios[0] = txtDNI;
        obligatorios[1] = txtNombre;


        return obligatorios;
    }

    private boolean altaCliente() {
        boolean ret = false;
        Date fecha = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
        try {
            fecha = sdf.parse(txtFechaNac.getText());

            try {
                if (misHerramientas.getDialogoConfirmacion().confirmar("¿Está seguro que desea dar de alta al cliente " + txtNombre.getText() + " " + txtApellido.getText() + " ?", "Confirmar Alta")) {
                    miControladora.getMiVistaCliente().crearCliente(txtDNI.getText(),txtApellido.getText(), fecha, txtNombre.getText(), txtDireccion.getText(), txtTelefono.getText(), txtEmail.getText());
                    cargarTabla();
                    ret = true;
                }
            } catch (PreexistingEntityException ex) {
                misHerramientas.getDialogoError().dialogoError("Cliente preexistente, verifique los datos.");
                Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                misHerramientas.getDialogoError().dialogoError(ex.getMessage());
                Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParseException e) {
            misHerramientas.getDialogoError().dialogoError("Fecha inválida: corrígala, por favor..");
        }
        return ret;
    }

    private boolean modificacionCliente() {
        boolean ret = false;
        Date fecha = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
        try {
            fecha = sdf.parse(txtFechaNac.getText());
            if (misHerramientas.getDialogoConfirmacion().confirmar("¿Está seguro que desea modificar al cliente " + txtNombre.getText() + " " + txtApellido.getText() + " ?", "Confirmar Modificación")) {
                try {
                    miControladora.getMiVistaCliente().modificarCliente(Integer.parseInt(txtNroCliente.getText()), txtApellido.getText(), txtDNI.getText(), fecha, txtNombre.getText(), txtDireccion.getText(), txtTelefono.getText(), txtEmail.getText());
                    ret = true;
                } catch (NonexistentEntityException ex) {
                    misHerramientas.getDialogoError().dialogoError("Cliente no Existente, verifique los datos.");
                    Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    misHerramientas.getDialogoError().dialogoError(ex.getMessage());
                    Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        } catch (ParseException e) {
            misHerramientas.getDialogoError().dialogoError("Fecha inválida: corrígala, por favor..");
        }
        return ret;
    }

    private void checkObligatorios() {
        if (goodToGo[0] && goodToGo[1]) {
            btnAlta.setEnabled(true);
        } else {
            btnAlta.setEnabled(false);
        }
    }
    
 

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SecaderoPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("SecaderoPU").createEntityManager();
        panelTabla = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCliente = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        panelAlta = new javax.swing.JPanel();
        lblNroCliente = new javax.swing.JLabel();
        txtNroCliente = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        lblNombre = new javax.swing.JLabel();
        txtApellido = new javax.swing.JTextField();
        lblApellido = new javax.swing.JLabel();
        lblFechaNac = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        lblTelefono = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        lblEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lblDNI = new javax.swing.JLabel();
        txtDNI = new javax.swing.JTextField();
        txtFechaNac = new javax.swing.JTextField();
        panelBotones = new javax.swing.JPanel();
        btnBaja = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnAlta = new javax.swing.JButton();
        btnCancelarSeleccion = new javax.swing.JButton();
        chkMostrarBajas = new javax.swing.JCheckBox();

        tblCliente.setAutoCreateRowSorter(true);
        tblCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N° Cliente", "Nombre", "Apelldio", "DNI", "Fecha de Nacimiento", "Dirección", "Teléfono", "E-Mail"
            }
        ));
        tblCliente.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblCliente.setColumnSelectionAllowed(true);
        tblCliente.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCliente.getTableHeader().setResizingAllowed(false);
        tblCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblClienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCliente);
        tblCliente.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout panelTablaLayout = new javax.swing.GroupLayout(panelTabla);
        panelTabla.setLayout(panelTablaLayout);
        panelTablaLayout.setHorizontalGroup(
            panelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
        );
        panelTablaLayout.setVerticalGroup(
            panelTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jScrollPane2.setAutoscrolls(true);

        lblNroCliente.setText("nroCliente");

        txtNroCliente.setEditable(false);
        txtNroCliente.setColumns(10);
        txtNroCliente.setText("<Autogenerado>");

        txtNombre.setColumns(10);
        txtNombre.setText("Luciano");
        txtNombre.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtNombreCaretUpdate(evt);
            }
        });

        lblNombre.setText("Nombre");

        txtApellido.setColumns(10);
        txtApellido.setText("Donati");

        lblApellido.setText("Apellido");

        lblFechaNac.setText("Fecha de Nacimiento");

        lblDireccion.setText("Direccion");

        txtDireccion.setColumns(10);
        txtDireccion.setText("Direccion1");

        lblTelefono.setText("Telefono");

        txtTelefono.setColumns(10);
        txtTelefono.setText("(3764) 843636");

        lblEmail.setText("E-mail");

        txtEmail.setColumns(10);
        txtEmail.setText("soy@tupadre.com.ar");
        txtEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtEmailKeyPressed(evt);
            }
        });

        lblDNI.setText("DNI");

        txtDNI.setColumns(10);
        txtDNI.setText("35194976");
        txtDNI.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtDNICaretUpdate(evt);
            }
        });

        txtFechaNac.setText("24/01/91");

        javax.swing.GroupLayout panelAltaLayout = new javax.swing.GroupLayout(panelAlta);
        panelAlta.setLayout(panelAltaLayout);
        panelAltaLayout.setHorizontalGroup(
            panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAltaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelAltaLayout.createSequentialGroup()
                        .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNroCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNroCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtDNI)
                                .addComponent(txtFechaNac))
                            .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        panelAltaLayout.setVerticalGroup(
            panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAltaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNroCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNroCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblApellido))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDNI))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFechaNac)
                    .addComponent(txtFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDireccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTelefono))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAltaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEmail))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jScrollPane2.setViewportView(panelAlta);

        btnBaja.setText("Baja");
        btnBaja.setEnabled(false);
        btnBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBajaActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnAlta.setText("Alta");
        btnAlta.setEnabled(false);
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        btnCancelarSeleccion.setText("Cancelar Selección");
        btnCancelarSeleccion.setEnabled(false);
        btnCancelarSeleccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarSeleccionActionPerformed(evt);
            }
        });

        chkMostrarBajas.setText("Mostrar Bajas");
        chkMostrarBajas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkMostrarBajasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnBaja)
                .addGap(18, 18, 18)
                .addComponent(btnModificar)
                .addGap(56, 56, 56)
                .addComponent(chkMostrarBajas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCancelarSeleccion)
                .addGap(244, 244, 244)
                .addComponent(btnAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBaja)
                    .addComponent(btnModificar)
                    .addComponent(btnAlta)
                    .addComponent(btnCancelarSeleccion)
                    .addComponent(chkMostrarBajas))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelTabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelTabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        boolean sinErrores = false;
        try {
            if (datosValidos(camposObligatorios())) {
                if (btnModificar.isEnabled()) {
                    sinErrores = modificacionCliente();
                } else {
                    sinErrores = altaCliente();
                }
                if (sinErrores) {
                    vaciarCampos();
                    cargarTabla();
                    btnAlta.setText("Alta");
                    btnModificar.setEnabled(false);
                }
            }
        } catch (Exception ex) {
            misHerramientas.getDialogoError().dialogoError(ex.getMessage());
            Logger
                    .getLogger(ABMClientes.class
                    .getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnAltaActionPerformed

    private void chkMostrarBajasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkMostrarBajasActionPerformed
        cargarTabla();
    }//GEN-LAST:event_chkMostrarBajasActionPerformed

    private void tblClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClienteMouseClicked
        filaSeleccionada = true;
        btnModificar.setEnabled(true);
        btnBaja.setEnabled(true);
        btnCancelarSeleccion.setEnabled(true);
    }//GEN-LAST:event_tblClienteMouseClicked

    private void btnCancelarSeleccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarSeleccionActionPerformed
        filaSeleccionada = false;
        vaciarCampos();
        btnCancelarSeleccion.setEnabled(false);
        btnModificar.setEnabled(false);
        btnAlta.setText("Alta");
    }//GEN-LAST:event_btnCancelarSeleccionActionPerformed

    private void txtNombreCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtNombreCaretUpdate
        if (txtNombre.getText().isEmpty()) {
            goodToGo[0] = false;
        } else {
            goodToGo[0] = true;
        }
        checkObligatorios();
    }//GEN-LAST:event_txtNombreCaretUpdate

    private void txtDNICaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtDNICaretUpdate
        if (txtDNI.getText().isEmpty()) {
            goodToGo[1] = false;
        } else {
            goodToGo[1] = true;
        }
        checkObligatorios();
    }//GEN-LAST:event_txtDNICaretUpdate

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        llenarCampos();
        btnAlta.setText("Guardar");
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBajaActionPerformed
        int i = tblCliente.getSelectedRow();
        System.out.println("emtrop");
        if (i != -1) {
            try {
                miControladora.getMiVistaCliente().eliminarCliente(txtDNI.getText());
            } catch (NonexistentEntityException ex) {
                misHerramientas.getDialogoError().dialogoError("Cliente no Existente, verifique los datos.");
                Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                misHerramientas.getDialogoError().dialogoError(ex.getMessage());
                Logger.getLogger(ABMClientes.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        cargarTabla();
    }//GEN-LAST:event_btnBajaActionPerformed

    private void txtEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEmailKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btnAlta.doClick();
        }
    }//GEN-LAST:event_txtEmailKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager SecaderoPUEntityManager;
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnBaja;
    private javax.swing.JButton btnCancelarSeleccion;
    private javax.swing.JButton btnModificar;
    private javax.swing.JCheckBox chkMostrarBajas;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblApellido;
    private javax.swing.JLabel lblDNI;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblFechaNac;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblNroCliente;
    private javax.swing.JLabel lblTelefono;
    private javax.swing.JPanel panelAlta;
    private javax.swing.JPanel panelBotones;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTable tblCliente;
    private javax.swing.JTextField txtApellido;
    private javax.swing.JTextField txtDNI;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFechaNac;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroCliente;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
